package com.niit.drugsmanager.controller;

import com.niit.drugsmanager.entity.AdministrativeRegion;
import com.niit.drugsmanager.service.AdministrativeRegionService;
import com.niit.drugsmanager.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.util.List;

@Controller
@RequestMapping("/api/region")
public class AdministrativeRegionController {
    @Autowired
    private AdministrativeRegionService administrativeRegionService;
    @Autowired
    private UserService userService;

    @PostMapping("/findRegion")
    public ResponseEntity<List<AdministrativeRegion>> findRegion(@RequestBody RequestRegion region) {
        try {
            if(region.pid == -1){
                return ResponseEntity.ok(administrativeRegionService.findAdministrativeRegionByLvl(1));
            }else {
                return ResponseEntity.ok(administrativeRegionService.findAllAdministrativeRegionsByParent(region.pid));
            }
        }catch (Exception e){
            return ResponseEntity.notFound().build();
        }
    }

    @Data
    public static class RequestRegion{
        private int pid;
    }
}
